package com.lab.util;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import com.lab.lang.DateUtils;

/**
 * Class description goes here.
 * 
 * @author nickevin@gmail.com
 * @version Jan 4, 2011 8:03:13 PM
 */

public class Timesheet {

    private Set<Sheet> sheets = new HashSet<Sheet>();

    public Timesheet() {}

    public Timesheet(Set<Sheet> sheets) {
        this.sheets = sheets;
    }

    public void generate(String tplFile, String targetFile) {
        try {
            VelocityContext ctx = new VelocityContext();
            ctx.put("sheets", sheets);
            ctx.put("currentDate", DateUtils.toString("yyyy-MM-dd")
                    + "T00:00:00.000");

            Writer writer = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(targetFile), "UTF-8"));

            Velocity.mergeTemplate(tplFile, "UTF-8", ctx, writer);

            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String getCurrentDate(String pattern) {
        return new SimpleDateFormat(pattern).format(new Date());
    }
}
